// subPages/center/communityActivities/communityActivities.js
const app = getApp();
var util =  require('../../../utils/util.js');
var loadMoreView,p = 1,num = app.globalData.pageNum,type = '社区活动'
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //社区id
    if(app.globalData.communityId){
      that.setData({
        communityId: app.globalData.communityId
      });
      that.getActivities()
    }else{
      app.communityIdCallback = communityId => {
        if (communityId != '') {
          that.setData({
            communityId: communityId
          });
          that.getActivities()
        }
      }
    }
  },

  //获取社员中心活动
  getActivities:function(){
    util.getAjax(app.globalData.apiUrl +'/get_community_member_center_activities',this.getActivity,{community_id: this.data.communityId,p: p,num: num},this.getActivityErr)
  },

  getActivity(res){
    console.log(res)
    var that = this 
    if(res.data.list.length != 0){
      var list = []
      if(p == 1) {
        list = res.data.list
      } else {
        list = that.data.activityList.concat(res.data.list)
      }
      for(var i in list){
        if(list[i].status == 0){
          var endTime = new Date(String(list[i].close_time).replace(/-/g,"/")).getTime() || [];//倒计时的结束时间
          var nowTime = new Date(util.formatTime(new Date()).substring(0,10)).getTime()
          var total_micro_second = endTime - nowTime
          if (total_micro_second <= 0) {
            list[i].status = 1
          }
        }
      }
      that.setData({
        isActivity: true,
        activityList: list
      })
    }else{
      that.setData({
        isActivity: false,
        activityList: []
      })
    }
    loadMoreView = this.selectComponent("#loadMoreView")
    loadMoreView.loadMoreComplete(res.data,type)
  },

  //get Arr Status
  getArr(){
    
  },

  //获取商品数据异常
  getActivityErr(err){
    if(p>1){
      loadMoreView.loadMoreFail()
    }else{
      util.showModal('提示','获取社区活动失败，请向上滑动屏幕')
      this.setData({
        isFail: true
      })
    }
  },

  loadMoreListener: function(e) {
    p += 1
    this.getActivities()
  },

  clickLoadMore: function(e) {
    this.getActivities()
  },

  signUp:function(e){
    var item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '../signUp/signUp?list=' + JSON.stringify(item) + '&type=' + 1,
    })
  },

  //回到顶部
  goTop: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 600) {
      this.setData({
        isTop: true
      });
    } else {
      this.setData({
        isTop: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isFail){
      this.getActivities()
      return
    }
    loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})